﻿using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI.Users
{
    public partial class UserRoleFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public UserRoleFrm()
        {
            InitializeComponent();
            LoadUser();
        }
        public void LoadUser()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = (from u in db.Users.ToList()
                                        select new
                                        {
                                            u.LoginName,
                                            u.Mobile,
                                            UserName = u.Name,
                                            u.NickName,
                                            u.RoleIdStr,
                                            u.Remark,
                                            u.CreateTime,
                                            Enabled = u.Enabled ? "是" : "否",
                                            UserId = u.Id
                                        }).ToList();
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = (from r in db.Roles.ToList()
                                        select new
                                        {
                                            RoleName = r.Name,
                                            RoleRemark = r.Remark,
                                            //HaveOrNot = list.Contains(r.Id.ToString()),
                                            RoleId = r.Id

                                        }).ToList();
        }
        private void addBtn_Click(object sender, EventArgs e)
        {
            var userfrm = new UserFrm().ShowDialog();
            if (userfrm == DialogResult.Cancel)
            {
                LoadUser();
            }

        }

        private void refreshBtn_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridViewRow row = dataGridView1.CurrentRow;
                if (dataGridView1.CurrentRow.Cells["UserId"].Value != null && (int)dataGridView1.CurrentRow.Cells["UserId"].Value > 0)
                {

                    var user = db.Users.Find((int)dataGridView1.CurrentRow.Cells["UserId"].Value);
                    if (user != null && dataGridView1.Columns[e.ColumnIndex].Name == "DeleteBtn")
                    {
                        DialogResult dialog = MessageBox.Show($"确定要删除用户{user.Name}吗？", "提示", MessageBoxButtons.OKCancel);
                        if (dialog == DialogResult.OK)
                        {
                            db.Users.Remove(user);
                            var result = db.SaveChanges();
                            MessageBox.Show("删除" + (result > 0 ? "成功" : "失败"));
                            LoadUser();
                            return;
                        }
                    }
                    else if (user != null && dataGridView1.Columns[e.ColumnIndex].Name == "ForbiddenBtn")
                    {

                        DialogResult dialog = MessageBox.Show($"确定要禁用用户{user.Name}吗？", "提示", MessageBoxButtons.OKCancel);
                        if (dialog == DialogResult.OK)
                        {
                            //db.Users.Remove(user);
                            user.Enabled = false;
                            var result = db.SaveChanges();
                            MessageBox.Show("禁用用户" + (result > 0 ? "成功" : "失败"));
                            LoadUser();
                            return;
                        }
                    }

                    if (user != null)// && !string.IsNullOrEmpty(user.RoleIdStr))
                    {

                        var list = (user.RoleIdStr == null ? "" : user.RoleIdStr).Split(',');
                        var roles = (from r in db.Roles.ToList()
                                         //where (list).Contains(r.Id.ToString())
                                     select new
                                     {
                                         RoleName = r.Name,
                                         RoleRemark = r.Remark,
                                         HaveOrNot = list.Contains(r.Id.ToString()),
                                         RoleId = r.Id

                                     }).ToList();
                        dataGridView2.AutoGenerateColumns = false;
                        dataGridView2.DataSource = roles;
                    }
                }


            }
        }
        private void UpdateGd2()
        {
            DataGridViewRow row = dataGridView1.CurrentRow;
            if (dataGridView1.CurrentRow.Cells["UserId"].Value != null && (int)dataGridView1.CurrentRow.Cells["UserId"].Value > 0)
            {

                var user = db.Users.Find((int)dataGridView1.CurrentRow.Cells["UserId"].Value);
                if (user != null)// && !string.IsNullOrEmpty(user.RoleIdStr))
                {

                    var list = (user.RoleIdStr == null ? "" : user.RoleIdStr).Split(',');
                    var roles = (from r in db.Roles.ToList()
                                     //where (list).Contains(r.Id.ToString())
                                 select new
                                 {
                                     RoleName = r.Name,
                                     RoleRemark = r.Remark,
                                     HaveOrNot = list.Contains(r.Id.ToString()),
                                     RoleId = r.Id

                                 }).ToList();
                    dataGridView2.AutoGenerateColumns = false;
                    dataGridView2.DataSource = roles;
                }
            }
        }
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridViewRow row = dataGridView1.CurrentRow;
                if (dataGridView1.CurrentRow.Cells["UserId"].Value != null && (int)dataGridView1.CurrentRow.Cells["UserId"].Value > 0)
                {

                    var user = db.Users.Find((int)dataGridView1.CurrentRow.Cells["UserId"].Value);
                    if (user != null)// && !string.IsNullOrEmpty(user.RoleIdStr))
                    {

                        var list = (user.RoleIdStr == null ? "" : user.RoleIdStr).Split(',');
                        if (dataGridView2.CurrentRow.Cells["RoleId"].Value != null && (int)dataGridView2.CurrentRow.Cells["RoleId"].Value > 0)
                        {
                            var roleId = dataGridView2.CurrentRow.Cells["RoleId"].Value.ToString();
                            var haveRole = dataGridView2.CurrentRow.Cells["HaveOrNot"].Value == null ? false : (bool)dataGridView2.CurrentRow.Cells["HaveOrNot"].Value;
                            if (!haveRole)
                            {
                                var strList = list.ToList();
                                strList.Add(roleId);
                                user.RoleIdStr = string.Join(",", strList);
                                db.SaveChanges();
                            }
                            else
                            {
                                var strList = list.Where(o => !o.Equals(roleId)).ToList();
                                user.RoleIdStr = string.Join(",", strList);
                                db.SaveChanges();
                            }
                            UpdateGd2();
                        }

                    }
                }
            }
        }
    }
}
